#include "stdafx.h"
#include "TextureCube.h"

#include "Log.h"
#include "Exception.h"
#include "MemUtils.h"
#include "ErrorHandlers.h"

namespace Cvekas {

TextureCube::TextureCube(ResourceId id, D3DDevicePtr device, const std::string& name, const std::string& filename)
: BaseTexture(id, device, name, D3DFMT_A8R8G8B8), filename(filename) // HACK: pass proper format
{
	is_ready = false;

	IDirect3DCubeTexture9* temp_texture = static_cast<IDirect3DCubeTexture9*>(texture);

	LOG(FORMAT("Loading cube texture %s from %s", % name % filename));
	E_E(D3DXCreateCubeTextureFromFileEx(device.get(), filename.c_str(), D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_FROM_FILE, D3DPOOL_MANAGED, 
		D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, &temp_texture ));
	E_E(temp_texture->GetLevelDesc(0, &description));
	format = description.Format;
	texture = temp_texture;
	is_ready = true;
	is_released = false;
}

TextureCube::~TextureCube()
{
	// Releasing is handled by BaseTexture
	if(!is_released)
		LOG(FORMAT("Texture %s released", % name));
}

void TextureCube::onDeviceLost()
{
	// Do nothing, cube textures are always in managed pool
}

void TextureCube::onDeviceReset()
{
	// Do nothing, cube textures are always in managed pool
}

}
